﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Morus.Presentation
{
    public static class CollectionExtensions
    {
        [DebuggerStepThrough]
        public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
        {
            Check.Argument.IsNotNull(source, "source");

            return source.ToDictionary(m => m.Key, m => m.Value);
        }

        [DebuggerStepThrough]
        public static bool SetEqual<T>(this IEnumerable<T> x, IEnumerable<T> y)
        {
            Check.Argument.IsNotNull(x, "x");
            Check.Argument.IsNotNull(y, "y");

            var objectsInX = x.ToList();
            var objectsInY = y.ToList();

            if (objectsInX.Count() != objectsInY.Count())
            {
                return false;
            }

            foreach (var objectInY in objectsInY)
            {
                if (!objectsInX.Contains(objectInY))
                {
                    return false;
                }

                objectsInX.Remove(objectInY);
            }

            return objectsInX.IsEmpty();
        }
    }
}
